package at.dcmspl;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.rmi.RemoteException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.EJBException;
import javax.ejb.SessionSynchronization;
import javax.ejb.Stateful;

/**
 * Manage the File Export
 * @author Hannes Stornig, Gosch Daniel
 */
@Stateful
public class DcmFileExport implements DcmFileExportRemote, SessionSynchronization{
    
    public static final String TEMP_EXTENSION = ".tmp";
    public static final String FINAL_EXTENSION = ".dcm";
    
    @Resource//Enivironment-Entrie(default Value... could be changed on deployment)
    String exportdirPath = "DicomFiles";
    String s = File.separator;
    
    List<File> exportfiles = new LinkedList<File>();

    @Override
    public String getFINAL_EXTENSION() {
        return FINAL_EXTENSION;
    }

    @Override
    public String getTEMP_EXTENSION() {
        return TEMP_EXTENSION;
    }
    
    @Override
    public String exportDcmFile(String dcmFileName) throws IOException {
        String dateiname = dcmFileName;
        
        File exportordner = new File (exportdirPath);
        exportordner.mkdir();        
        File exportfile = new File(exportdirPath  + s +dateiname+TEMP_EXTENSION);
        File exportfile_dcm = new File(exportdirPath  + s +dateiname+FINAL_EXTENSION);
        if(!exportfile.exists() && !exportfile_dcm.exists()){
            exportfile.createNewFile();
            exportfiles.add(exportfile);
            PrintStream ps = new PrintStream(exportfile);
            ps.println(dcmFileName);
            ps.close();
            return dateiname;
        }else{
            return null;
        }
    }
        
    @Override
    public void removeDcmFile(String dcmFileName) throws IOException{
        File f = new File(exportdirPath+s+dcmFileName);
        Logger.getLogger(DcmFileExport.class.getName()).log(Level.INFO, "###Delete {0}",f);
        if (f.exists()) {
            f.delete(); 
            Logger.getLogger(DcmFileExport.class.getName()).log(Level.INFO, "###{0} successfully deleted",f);
        }else{
            Logger.getLogger(DcmFileExport.class.getName()).log(Level.INFO, "###Deleting of DcmFile {0} not successful",f);
        }
    }
    
    @Override
    public void afterBegin() throws EJBException, RemoteException {
	exportfiles.clear();
    }
    
    /**
    * Change Fileextension from tmp to dcm
    * @throws EJBException
    * @throws RemoteException 
    */
    @Override
    public void beforeCompletion() throws EJBException, RemoteException {
        for (int d = 0; d < exportfiles.size(); d++) {
            File exportdatei = exportfiles.get(d);
            File renamedFile = new File(exportdatei.getPath().replace(TEMP_EXTENSION, FINAL_EXTENSION));
            exportdatei.renameTo(renamedFile);
            exportfiles.set(d, renamedFile);
        }
    }
    
    /**
     * Delete all temporary saved Files in case of an Rollback
     * @param commit
     * @throws EJBException
     * @throws RemoteException 
     */
    @Override
    public void afterCompletion(boolean commit) throws EJBException, RemoteException {
        if (!commit) {
            for (File exportdatei: exportfiles)
                exportdatei.delete();
        }
    }
    
}
